package backend.service.admin;

import backend.model.admin.Employee;
import backend.service.base.DBModelServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl extends DBModelServiceImpl<Employee> implements EmployeeService {

    @Override
    public Class<Employee> getEntityClass() {
        return Employee.class;
    }

	@Override
    public List<Employee> findAllEmployees() {
		return dbModelDao.findByNamedQuery("findAllEmployees");
	}

    public List<Employee> findAllEmployeesByForeman(int  foremanId) {
        List<Employee> employees = dbModelDao.findByNamedQueryAndParams("findAllEmployeesByForeman",
                new String[] {"foremanId"}, new Object[] {foremanId});
        if (employees == null) {
            return Collections.emptyList();
        }
        return employees;
    }

    @Override
    public List<Employee> findAllActiveEmployees() {
        return dbModelDao.findByNamedQuery("findAllActiveEmployees");
    }

    @Override
    public Employee findEmployeeByFirstAndLastName(String firstName, String lastName) {
        List<Employee> employees = dbModelDao.findByNamedQueryAndParams("findEmployeeByFirstAndLastName",
                new String[] {"firstName", "lastName"}, new Object[] {firstName, lastName});
        assert employees.size() <= 1;
        if (employees.size() == 1) {
            return employees.get(0);
        }
        return null;
    }
    @Override
    public Employee findActiveEmployeeByIdentificationCard(String identificationCard) {
        List<Employee> employees = dbModelDao.findByNamedQueryAndParams("findActiveEmployeeByIdentificationCard",
                new String[] {"identificationCard"}, new Object[] {identificationCard});
        assert employees.size() <= 1;
        if (employees.size() == 1) {
            return employees.get(0);
        }
        return null;
    }

    @Override
    public List<Employee> findAllActiveEmployeesByTeam(int teamId) {
        List<Employee> employees = dbModelDao.findByNamedQueryAndParams("findAllActiveEmployeesByTeam",
                new String[] {"teamId"}, new Object[] {teamId});
        return employees;
    }
}
